<?php
/**
 * 过滤用户产生的输入
 * 增强安全
 */
function strip_input($input) {
    if (is_int($input)) {
        return intval($input);
    }

    if (is_string($input)) {
        return htmlspecialchars(addslashes($input));
    }

    if (is_array($input)) {
        $ret = array();
        foreach ($input as $key => $value) {
            $key = strip_input($key);
            $value = strip_input($value);

            $ret[$key] = $value;
        }

        return $ret;
    }

    return $input;
}

/**
 * 产生全站统一的带有Query的URL
 */
function url($data, $route) {
    $route_parts = array('c', 'a', 'm');
    foreach ($route_parts as $r) {
        $route[$r] = isset($route[$r]) ? $route[$r] : "";
    }

    $route = route($route['c'], $route['a'], $route['m']);
    $data = http_build_query($data);

    $url = $route . "&" . $data;
    return $url;
}

/**
 * 产生全站统一的ROUTE
 */
function route($controller = "", $action = "", $module = "") {
    if (empty($controller)) {
        $controller = \lightblog::$app->request->controller;
    }

    if (empty($action)) {
        $action = \lightblog::$app->request->action;
    }

    if (empty($module)) {
        $module = \lightblog::$app->request->module;
    }

    return "index.php?r={$module}/{$controller}/{$action}";
}

/**
 * 把new_post这样的字符串转化为newPost这样的字符串
 */
function name_convert($name) {
    $name = "a" . $name;
    $name = str_replace('_', ' ', $name);
    $name = ucwords($name);
    $name = str_replace(' ', '', $name);

    return substr($name, 1);
}
?>
